package com.example.administrator.baikangxing.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.baikangxing.Dao.OperateData;
import com.example.administrator.baikangxing.MyApplication;
import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.config.Constants;
import com.example.administrator.baikangxing.config.Url;
import com.example.administrator.baikangxing.ronglian.SDKCoreHelper;
import com.example.administrator.baikangxing.utils.HttpUtil;
import com.example.administrator.baikangxing.utils.LogUtil;
import com.example.administrator.baikangxing.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class SettingActivity extends BaseActivity implements View.OnClickListener {


    private LinearLayout setting_ll_feed_back;
    private Button setting_bt_out_login;
    private LinearLayout setting_ll_about;
    private LinearLayout setting_ll_share;
    private LinearLayout setting_ll_problem;
    private LinearLayout setting_ll_upgrade;


    @Override
    public int getContentViewLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        setTitles("系统设置");
        base_ib_menu.setVisibility(View.INVISIBLE);
        setting_ll_feed_back = findView(R.id.setting_ll_feed_back);
        setting_ll_about = findView(R.id.setting_ll_about);
        setting_bt_out_login = findView(R.id.setting_bt_out_login);
        setting_ll_share = findView(R.id.setting_ll_share);
        setting_ll_problem = findView(R.id.setting_ll_problem);
        setting_ll_upgrade = findView(R.id.setting_ll_upgrade);

        setting_ll_upgrade.setOnClickListener(this);
        setting_ll_problem.setOnClickListener(this);
        setting_ll_share.setOnClickListener(this);
        setting_bt_out_login.setOnClickListener(this);
        setting_ll_feed_back.setOnClickListener(this);
        setting_ll_about.setOnClickListener(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_ll_feed_back://意见反馈
                startActivity(new Intent(this, FeedBackActivity.class));
                break;
            case R.id.setting_bt_out_login://退出登录
                showOutDialog();
                break;
            case R.id.setting_ll_about://关于我们
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case R.id.setting_ll_share://分享客户端
                startActivity(new Intent(this, ShareActivity.class));
                break;
            case R.id.setting_ll_problem://常见问题
                break;
            case R.id.setting_ll_upgrade://检查更新
                break;
            default:
                break;
        }
    }

    private void showOutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("是否退出登录");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                loginOut();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.create().show();
    }

    private void loginOut() {//退出登录
        String[] values = new String[]{OperateData.getStringData(SettingActivity.this, Constants.USER_NAME),"ext"};
        HttpUtil.getInstance().postString(Url.login_out_url, new String[]{"user","ext"}, values, new HttpUtil.CallBackPostString() {
            @Override
            public void onSuccess(JSONObject successString) {
                try {
                    String errorCode = successString.getString("errorCode");
                    if(errorCode.equals("0")){
                        OperateData.saveIntData(SettingActivity.this,Constants.USER_STATE, 0);
                        new SDKCoreHelper(SettingActivity.this).logout();
                        ToastUtil.showToast(getString(R.string.login_out_success_hint));
                        MyApplication.getInstance().destory();
                        startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                    }else {
                        ToastUtil.showToast(getString(R.string.login_out_fail_hint));
                        LogUtil.e(getString(R.string.login_out_fail_reason)+successString.getString("errorInfo"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(JSONObject failString) {

            }

            @Override
            public void onProgress(long bytesWritten, long totalSize) {

            }
        });
    }
}
